package com.cskaoyan.order.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.order.constant.OrderRetCode;
import com.cskaoyan.order.converter.OrderConverter;
import com.cskaoyan.order.converter.OrderConverterImpl;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.entitys.OrderShipping;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.OrderShippingMapper;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.service.OrderQueryService;
import com.cskaoyan.user.constants.UserRetCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;

/**
 * @author ：王李轶群
 * @description：TODO
 * @date ：5/19/22 4:07 PM
 */
@Slf4j
@Service
public class OrderQueryServiceImpl implements OrderQueryService {
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Autowired
    OrderConverter orderConverter;

    @Override
    public OrderListResponse orderList(OrderListRequest request) {

        Example example = new Example(Order.class);
        example.createCriteria().andEqualTo("userId",request.getUserId());
        example.setOrderByClause("create_time DESC");
        PageHelper.startPage(request.getPage(),request.getSize());
        List<Order> orders = orderMapper.selectByExample(example);
        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);
        long total = orderPageInfo.getTotal();
        OrderListResponse orderListResponse = new OrderListResponse();
        LinkedList<OrderDetailInfo> orderDetailInfos = new LinkedList<>();
        for (Order order1 : orders) {
            OrderDetailInfo orderDetailInfo = orderConverter.order2detail(order1);

            String orderId = order1.getOrderId();
            Example example1 = new Example(OrderItem.class);
            example1.createCriteria().andEqualTo("orderId",orderId);
            List<OrderItem> orderItems = orderItemMapper.selectByExample(example1);
            List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);
            orderDetailInfo.setOrderItemDto(orderItemDtos);

            Example example2 = new Example(OrderShipping.class);
            example2.createCriteria().andEqualTo("orderId",orderId);
            List<OrderShipping> orderShippings = orderShippingMapper.selectByExample(example2);
            OrderShipping orderShipping1 = orderShippings.get(0);
            OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShipping1);
            orderDetailInfo.setOrderShippingDto(orderShippingDto);

            orderDetailInfos.add(orderDetailInfo);
        }
        orderListResponse.setData(orderDetailInfos);
        orderListResponse.setTotal(total);

        return orderListResponse;
    }

    @Override
    public OrderDetailResponse orderDetail(OrderDetailRequest request) {
        return null;
    }


    @Override
    public OrderDetailResponseVO getOrderDetail(OrderDetailRequest request) {

        OrderDetailResponseVO orderDetailResponseVo = new OrderDetailResponseVO();

        try{
            // 先做参数校验
            request.requestCheck();

        //获取GoodsList
        List<OrderItemDto> orderItemDtoList = orderItemMapper.getOrderDetail(request.getOrderId());
        orderDetailResponseVo.setGoodsList(orderItemDtoList);

        //传入status
        Order order = orderMapper.queryOneOrder(request.getOrderId().trim());
        orderDetailResponseVo.setOrderStatus(order.getStatus());

        //传入Ordertotal
            BigDecimal orderTotal = new BigDecimal(0);
            for (int i = 0; i < orderItemDtoList.size(); i++) {
                orderTotal.add(orderItemDtoList.get(i).getTotalFee());
            }
            orderDetailResponseVo.setOrderTotal(orderTotal);

        //传入tel,streetName
        OrderShipping orderShipping = orderShippingMapper.queryShippingById(request.getOrderId().trim());
        orderDetailResponseVo.setStreetName(orderShipping.getReceiverAddress());
        orderDetailResponseVo.setTel(orderShipping.getReceiverPhone());
        orderDetailResponseVo.setUserName(orderShipping.getReceiverName());

        //传入code,msg
        orderDetailResponseVo.setCode(OrderRetCode.SUCCESS.getCode());
        orderDetailResponseVo.setMsg(OrderRetCode.SUCCESS.getMessage());

        }catch (Exception e){
            log.error("OrderQueryServiceImpl.getOrderDetail occur Exception :"+e);
            ExceptionProcessorUtils.wrapperHandlerException(orderDetailResponseVo,e);
        }
        return orderDetailResponseVo;
    }
}
